//
//  ZCXActivityViewController.m
//  LoveOfPet
//
//  Created by dllo on 15/11/13.
//  Copyright © 2015年 孔维. All rights reserved.
//

#import "ZCXActivityViewController.h"
#import "ZCXActivityTableViewCell.h"
#import "ZFHYNetWork.h"
#import "ActivityModel.h"
#import "ZCXActivityDetialViewController.h"
#import "FileManage.h"
#import <SVProgressHUD.h>

#define HEIGHT  [[UIScreen mainScreen]bounds].size.height / 667
#define WIDTH   [[UIScreen mainScreen]bounds].size.width / 375
@interface ZCXActivityViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSArray *responseArray;
@property (nonatomic, retain) NSMutableArray *activityArray;
@property (nonatomic, retain) NSString *path;
@end

@implementation ZCXActivityViewController

- (void)dealloc{
    [_tableView release];
    [_responseArray release];
    [_activityArray release];
    [_path release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"萌宠话题";
    self.view.backgroundColor = [UIColor whiteColor];
     self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    //文件缓存
    self.activityArray = [NSMutableArray array];
    self.path = [FileManage createFile:@"activity.plist"];
    NSDictionary *dic = [FileManage readFile:self.path];
    self.responseArray = [dic objectForKey:@"response"];
    for (NSDictionary *dicc in self.responseArray) {
        ActivityModel *activity = [ActivityModel ActivityModelWithDictionary:dicc];
        [self.activityArray addObject:activity];
    }
    [self netWorkingWithString:@"http://bbs.chinapet.com/plugin.php?pagesize=10&page=1&action=hottaglist&id=leepet_thread%3Aapi&version=d1bd83a33f1a841ab7fda32449746cc4"];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64 , self.view.frame.size.width, self.view.frame.size.height - 64 * HEIGHT) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = NO;
    //注册Cell
    [self.tableView registerClass:[ZCXActivityTableViewCell class] forCellReuseIdentifier:@"activitytableviewcell"];
    [self.view addSubview:self.tableView];
    self.tableView.tableFooterView = [[[UIView alloc]initWithFrame:CGRectZero]autorelease];
    [_tableView release];
}
#pragma mark 数据请求
- (void)netWorkingWithString:(NSString *)urlString{
    
    [ZFHYNetWork handleGETWithUrlString:urlString parameters:nil showHuD:YES onView:nil successfulBlock:^(id responseObject) {
        NSDictionary *dic = responseObject;
        [FileManage writeFile:dic andFilePath:self.path];
        
        self.responseArray = [dic objectForKey:@"response"];
        for (NSDictionary *dicc in self.responseArray) {
            ActivityModel *activity = [ActivityModel ActivityModelWithDictionary:dicc];
            [self.activityArray addObject:activity];
        }
        [self.tableView reloadData];
    } failureBlock:^(NSError *error) {
        NSLog(@"失败");
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.responseArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZCXActivityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"activitytableviewcell" forIndexPath:indexPath];
    cell.activity = self.activityArray[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150 * HEIGHT;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ZCXActivityDetialViewController *activityDetial = [[ZCXActivityDetialViewController alloc]init];
    activityDetial.activity = self.activityArray[indexPath.row];
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:activityDetial animated:YES];
    [activityDetial release];
}
- (void)viewWillDisappear:(BOOL)animated{
    [SVProgressHUD dismiss];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
